<template>
	<view class="ys">
		<y-header></y-header>
		<view class="login">
			<view class="pc">
				<y-bread :name="pageName"></y-bread>
			</view>
			<view class="loginbox">
				<view class="infobox">
					<view class="inputli">
						<view class="input-text">
							{{$t('index.zh')}}
						</view>
						<view class="input-body">
							<uni-icons type="person"></uni-icons>
							<input type="text" :placeholder="$t('index.qsrndzh')" v-model="username">
						</view>
					</view>

					<view class="inputli" v-show="show_email">
						<view class="input-text">
							{{$t('index.email')}}
						</view>
						<view class="input-body">
							<uni-icons type="email"></uni-icons>
							<input type="text" :placeholder="$t('index.qsrndyx')" v-model="email" disabled>
						</view>
					</view>
					<view class="inputli">
						<view class="input-text">
							{{$t('index.yxyzm')}}
						</view>
						<view class="input-body">
							<uni-icons type="mail-open"></uni-icons>
							<input type="text" :placeholder="$t('index.qsryxyzm')" v-model="code">
							<button class="code" @tap="SendCode" v-show="is_send">{{$t('index.hqyzm')}}</button>
							<button class="code" v-show="!is_send">{{time}}s</button>
						</view>
					</view>
					<view class="inputli">
						<view class="input-text">
							{{$t('index.password')}}
						</view>
						<view class="input-body">
							<uni-icons type="locked"></uni-icons>
							<input type="password" :placeholder="$t('index.qsrndmm')" v-model="password">
						</view>
					</view>
					<view class="btn">
						<button :disabled="loading" :loading="loading" @click="forget">{{$t('index.zhmm')}}</button>
					</view>
					<view class="tips">

					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		forget,
		emailSend
	} from "@/api/api.js"
	export default {
		data() {
			return {
				pageName: this.$t('index.zhmm'),
				loading: false,
				username: '',
				email: '',
				code: '',
				password: '',
				time: '',
				is_send: true,
				timer: '',
				show_email: false
			}
		},
		onLoad() {},
		methods: {
			toPage(url) {
				uni.navigateTo({
					url
				})
			},
			SendCode() {
				if (this.username == '') {
					uni.$showMsg(this.$t('index.qsrndzh'))
					return
				}
				this.is_send = false
				this.time = 60
				emailSend({
					type: "forget",
					username: this.username,
					email: this.email
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						this.email = res.data.email
						this.show_email = true
					}
				})
				this.timer = setInterval(() => {
					this.time--;
					if (this.time < 0) {
						clearInterval(this.timer)
						this.is_send = true
					}
				}, 1000)
			},
			forget() {
				let that = this
				if (that.username == '') {
					return uni.$showMsg(this.$t('index.qsrndzh'))
				}
				if (that.password == '') {
					return uni.$showMsg(this.$t('index.qsrxmm'))
				}
				that.loading = true
				forget({
					username: that.username,
					password: that.password,
					email: that.email,
					code: that.code
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/login/index'
							})
						}, 500)
					} else {
						that.loading = false
					}
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		max-width: 1180px;
		margin: 0 auto;
		width: 100%;
		padding: 10px;
		box-sizing: border-box;
	}

	.loginbox {
		margin-left: auto;
		margin-right: auto;
		max-width: 610px;
		border: 1px solid #dddddd;
		padding: 30px 35px 0px;
		box-sizing: border-box;
		width: 96%;
	}

	.tsbox {
		width: 96%;
		margin-left: auto;
		margin-right: auto;
		max-width: 610px;
		border: 1px solid #dddddd;
		padding: 10px 30px;
		box-sizing: border-box;
		font-size: 14px;
		margin-top: 20px;
		color: #999999;

		.ts {
			color: #ff5353;
			line-height: 2;
		}
	}

	.res {
		text-align: center;
		font-size: 14px;
		margin-bottom: 20px;

		text {
			color: #428bca;
			cursor: pointer;
			margin-left: 2px;
		}
	}

	.btn {
		padding: 0 10px;
		margin-top: 15px;

		button {
			background: #ff5353;
			color: #fff;
			height: 40px;
			line-height: 40px;
			font-size: 14px;
		}
	}

	.tips {
		color: #606060;
		margin: 15px 0px;
		font-size: 11px;
		line-height: 16px;
		padding: 0 10px;

		label {
			display: flex;
			align-items: flex-start;
			background: #fffbeb;
			padding: 10px 12px;
			line-height: 1.8;

			checkbox {
				transform: scale(.7);
			}

			text {
				color: #428bca;
			}
		}
	}

	.centertips {
		padding: 0 10px;
		font-size: 12px;
		color: #ff5353;
	}

	.inputli {
		padding: 10px;

		.input-text {
			font-size: 14px;
			color: #333333;
			margin-bottom: 10px;
		}

		.input-body {
			border: 1px solid #ccc;
			border-radius: 5px;
			display: flex;
			align-items: center;
			padding: 7px;
			box-sizing: border-box;

			input {
				padding-left: 5px;
				font-size: 14px;
				box-sizing: border-box;
				flex: 1;
			}

			button {
				font-size: 12px;
				background: #ff5353;
				color: #fff;
				width: 90px;
			}
		}
	}

	@media (max-width:768px) {
		.loginbox {
			padding: 10px;
			margin-top: 30px;
		}

		.pc {
			display: none;
		}

		.tsbox {
			padding: 10px;
		}
	}
</style>